<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form" action="" lay-filter="search-form">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">任务名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="docName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn" lay-submit lay-filter="data-search-btn">
                                <i class="layui-icon layui-icon-search"></i>搜 索
                            </button>
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-reset-btn">
                                <i class="layui-icon layui-icon-refresh"></i> 重 置
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <div class="layuimini-table">
            <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        </div>
    </div>
</div>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm data-add-btn" lay-event="add"> 新增</button>
    </div>
</script>

<script type="text/html" id="currentTableBar">
    <a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
</script>

<script type="text/html" id="addDialog">
    <div class="layuimini-form">
        <form class="layui-form" action="" lay-filter="add-form" id="add-form">
            <div class="layui-form-item">
                <label class="layui-form-label required">推送任务名称</label>
                <div class="layui-input-block">
                    <label>
                         <input type="text" name="docName" lay-verify="required" lay-reqtext="推送任务名称不能为空" placeholder="请输入推送任务名称" value="" class="layui-input">
                    </label>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">文件获取方式</label>
                <div class="layui-input-block">
                    <label>
                        <select name="srcLoc" lay-filter="srcLoc" lay-verify="required">
                            <option value="0">请选择</option>
                            <option value="1">本地获取</option>
                            <option value="2">FTP传输(bin)</option>
                            <option value="3">FTP传输(ASCII)</option>
                            <option value="4">SFTP传输</option>
                        </select>
                    </label>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">存放路径</label>
                <div class="layui-input-block">
                    <label>
                        <input type="text" name="srcPath" lay-verify="required" lay-reqtext="存放路径不能为空"
                               placeholder="请输入存放路径" value="" class="layui-input">
                    </label>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">文件名</label>
                <div class="layui-input-block">
                    <label>
                        <input type="text" name="fileName" lay-verify="required" lay-reqtext="文件名不能为空"
                               placeholder="请输入文件名" value="" class="layui-input">
                    </label>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">存放主机ip</label>
                <div class="layui-input-block">
                    <label>
                        <input type="text" name="srcHost"
                               placeholder="请输入存放主机ip" value="" class="layui-input">
                    </label>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">存放主机端口</label>
                <div class="layui-input-block">
                    <label>
                        <input type="text" name="srcPort"
                               placeholder="请输入存放主机端口" value="" class="layui-input">
                    </label>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">存放主机登录用户名</label>
                <div class="layui-input-block">
                    <label>
                        <input type="text" name="srcUsername"
                               placeholder="请输入存放主机登录用户名" value="" class="layui-input">
                    </label>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">存放主机登录密码</label>
                <div class="layui-input-block">
                    <label>
                        <input type="text" name="srcPassword"
                               placeholder="请输入存放主机登录密码" value="" class="layui-input">
                    </label>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">推送主机ip</label>
                <div class="layui-input-block">
                    <label>
                        <input type="text" name="tgtHost"  lay-reqtext="推送主机ip不能为空"
                               placeholder="请输入推送主机ip" value="" class="layui-input">
                    </label>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">推送主机端口</label>
                <div class="layui-input-block">
                    <label>
                        <input type="text" name="tgtPort" lay-reqtext="推送主机端口不能为空"
                               placeholder="请输入推送主机端口" value="" class="layui-input">
                    </label>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">文件推送方式</label>
                <div class="layui-input-block">
                    <label>
                        <select name="tgtLoc" lay-filter="tgtLoc" lay-verify="required">
                            <option value="0">请选择</option>
                            <option value="1">本地获取</option>
                            <option value="2">FTP传输(bin)</option>
                            <option value="3">FTP传输(ASCII)</option>
                            <option value="4">SFTP传输</option>
                            <option value="5">删除文件</option>
                        </select>
                    </label>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">目标主机登录用户名</label>
                <div class="layui-input-block">
                    <label>
                        <input type="text" name="tgtUsername" lay-reqtext="目标主机登录用户名不能为空"
                               placeholder="请输入目标主机登录用户名" value="" class="layui-input">
                    </label>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">目标主机登录密码</label>
                <div class="layui-input-block">
                    <label>
                        <input type="text" name="tgtPassword" lay-reqtext="目标主机登录密码不能为空"
                               placeholder="请输入目标主机登录密码" value="" class="layui-input">
                    </label>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">文件推送路径</label>
                <div class="layui-input-block">
                    <label>
                        <input type="text" name="tgtPath" lay-reqtext="文件推送路径不能为空"
                               placeholder="请输入文件推送路径" value="" class="layui-input">
                    </label>
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">备注信息</label>
                <div class="layui-input-block">
                    <textarea name="remark" class="layui-textarea" placeholder="请输入备注信息"></textarea>
                </div>
            </div>
            <div class="layui-form-item text-right">
                <button class="layui-btn" lay-submit lay-filter="addSaveBtn">保存</button>
                <button class="layui-btn layui-btn-primary" type="button" lay-filter="closeAddDialog">取消</button>
            </div>
        </form>
    </div>
</script>


<script type="text/html" id="editDialog">
    <div class="layuimini-form">
        <form class="layui-form" action="" lay-filter="edit-form" id="edit-form">
            <input type="hidden" name="id">
            <div class="layui-form-item">
                <label class="layui-form-label required">推送任务名称</label>
                <div class="layui-input-block">
                    <label>
                        <input type="text" name="docName" lay-verify="required" lay-reqtext="推送任务名称不能为空" placeholder="请输入推送任务名称" value="" class="layui-input">
                    </label>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">文件获取方式</label>
                <div class="layui-input-block">
                    <label>
                        <select name="srcLoc" lay-filter="srcLoc" lay-verify="required">
                            <option value="0">请选择</option>
                            <option value="1">本地获取</option>
                            <option value="2">FTP传输(bin)</option>
                            <option value="3">FTP传输(ASCII)</option>
                            <option value="4">SFTP传输</option>
                        </select>
                    </label>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">存放路径</label>
                <div class="layui-input-block">
                    <label>
                        <input type="text" name="srcPath" lay-verify="required" lay-reqtext="存放路径不能为空"
                               placeholder="请输入存放路径" value="" class="layui-input">
                    </label>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">文件名</label>
                <div class="layui-input-block">
                    <label>
                        <input type="text" name="fileName" lay-verify="required" lay-reqtext="文件名不能为空"
                               placeholder="请输入文件名" value="" class="layui-input">
                    </label>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">存放主机ip</label>
                <div class="layui-input-block">
                    <label>
                        <input type="text" name="srcHost"
                               placeholder="请输入存放主机ip" value="" class="layui-input">
                    </label>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">存放主机端口</label>
                <div class="layui-input-block">
                    <label>
                        <input type="text" name="srcPort"
                               placeholder="请输入存放主机端口" value="" class="layui-input">
                    </label>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">存放主机登录用户名</label>
                <div class="layui-input-block">
                    <label>
                        <input type="text" name="srcUsername"
                               placeholder="请输入存放主机登录用户名" value="" class="layui-input">
                    </label>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">存放主机登录密码</label>
                <div class="layui-input-block">
                    <label>
                        <input type="text" name="srcPassword"
                               placeholder="请输入存放主机登录密码" value="" class="layui-input">
                    </label>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">推送主机ip</label>
                <div class="layui-input-block">
                    <label>
                        <input type="text" name="tgtHost" lay-reqtext="推送主机ip不能为空"
                               placeholder="请输入推送主机ip" value="" class="layui-input">
                    </label>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">推送主机端口</label>
                <div class="layui-input-block">
                    <label>
                        <input type="text" name="tgtPort" lay-reqtext="推送主机端口不能为空"
                               placeholder="请输入推送主机端口" value="" class="layui-input">
                    </label>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">文件推送方式</label>
                <div class="layui-input-block">
                    <label>
                        <select name="tgtLoc" lay-filter="tgtLoc">
                            <option value="0">请选择</option>
                            <option value="1">本地获取</option>
                            <option value="2">FTP传输(bin)</option>
                            <option value="3">FTP传输(ASCII)</option>
                            <option value="4">SFTP传输</option>
                            <option value="4">删除文件</option>
                        </select>
                    </label>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">目标主机登录用户名</label>
                <div class="layui-input-block">
                    <label>
                        <input type="text" name="tgtUsername"
                               placeholder="请输入目标主机登录用户名" value="" class="layui-input">
                    </label>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">目标主机登录密码</label>
                <div class="layui-input-block">
                    <label>
                        <input type="text" name="tgtPassword"
                               placeholder="请输入目标主机登录密码" value="" class="layui-input">
                    </label>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">文件推送路径</label>
                <div class="layui-input-block">
                    <label>
                        <input type="text" name="tgtPath" lay-verify="required" lay-reqtext="文件推送路径不能为空"
                               placeholder="请输入文件推送路径" value="" class="layui-input">
                    </label>
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">备注信息</label>
                <div class="layui-input-block">
                    <textarea name="remark" class="layui-textarea" placeholder="请输入备注信息"></textarea>
                </div>
            </div>
            <div class="layui-form-item text-right">
                <button class="layui-btn" lay-submit lay-filter="editSaveBtn">保存</button>
                <button class="layui-btn layui-btn-primary" type="button" lay-filter="closeEditDialog">取消</button>
            </div>
        </form>
    </div>
</script>

<script>
    layui.use(['form', 'table', 'miniAjax'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniAjax = layui.miniAjax;

        var urlRule = /(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/;


        var currTable = table.render({
            elem: '#currentTableId',
            url: '/doc/query',
            method: 'post',
            contentType: 'application/json',
            request: {
                pageName: 'pageNo',
                limitName: 'pageSize'
            },
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "count": res.data.totalCount,
                    "data": res.data.records
                };
            },
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [[
                {field: 'id', width: 50, title: 'ID', align: 'center', fixed: 'left'},
                {field: 'docName', minWidth: 110, title: '文件名称'},
                {field: 'srcHost', minWidth: 110, title: '文件存放主机IP'},
                {field: 'srcPort', title: '文件存放主机端口', minWidth: 150},
                {field: 'srcPath', minWidth: 110, title: '文件存放路径'},
                {field: 'srcLoc', title: '文件获取方式', minWidth: 150, templet: function (d) {
                    switch (d.srcLoc) {
                        case 1:
                            return "本地获取"
                        case 2:
                            return "FTP传输(bin)"
                        case 3:
                            return "FTP传输(ASCII)"
                        case 4:
                            return "SFTP传输"
                        default:
                            return ""
                    }
                    }},
                {field: 'srcUsername', minWidth: 100, title: '存放主机用户名'},
                {field: 'srcPassword', title: '存放主机密码', minWidth: 150},
                {field: 'fileName', minWidth: 120, title: '存放文件名'},
                {field: 'tgtHost', minWidth: 110, title: '推送主机IP'},
                {field: 'tgtPort', title: '推送主机端口', minWidth: 150},
                {field: 'tgtLoc', minWidth: 110, title: '推送文件方式', templet: function (d) {
                        switch (d.tgtLoc) {
                            case 1:
                                return "本地存储"
                            case 2:
                                return "FTP传输(bin)"
                            case 3:
                                return "FTP传输(ASCII)"
                            case 4:
                                return "SFTP传输"
                            case 5:
                                return "删除文件"
                            default:
                                return ""
                        }
                    }},
                {field: 'tgtPath', title: '文件推送路径', minWidth: 150},
                {field: 'tgtUsername', minWidth: 110, title: '目标主机用户名'},
                {field: 'tgtPassword', title: '目标主机密码', minWidth: 150},
                {field: 'createdBy', minWidth: 75, title: '创建人'},
                {field: 'createdAt', title: '创建时间', minWidth: 150},
                {field: 'updatedBy', minWidth: 75, title: '更新人'},
                {field: 'updatedAt', title: '更新时间', minWidth: 150},
                {field: 'remark', minWidth: 120, title: '备注'},
                {title: '操作', minWidth: 120, toolbar: '#currentTableBar', align: "center"}
            ]],
            autoSort: false,
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            // 执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                },
                where: data.field
            });
            return false;
        });

        // 监听重置操作
        form.on('submit(data-reset-btn)', function (data) {
            form.val("search-form", {
                "projectId": ''
            });
            // 执行搜索重载
            currTable.reload({
                page: {
                    curr: 1
                },
                where: form.val("search-form")
            });
            return false;
        });

        /**
         * toolbar事件监听
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') { // 监听添加操作
                var index = layer.open({
                    title: '新增任务',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: false,
                    area: '580px', // 宽度580, 高度自适应
                    content: $('#addDialog').html(),
                    success: function (layero, dIndex) {

                        form.render('select', 'add-form');

                        // 表单提交事件
                        form.on('submit(addSaveBtn)', function (data) {
                            console.log(JSON.stringify(data.field));

                            miniAjax.postJSON({
                                url: '/doc/add',
                                data: JSON.stringify(data.field),
                                success: function (res) {
                                    if (res.code == '0') {
                                        layer.msg(res.msg, {
                                            time: 1000,
                                            icon: 1
                                        }, function () {
                                            layer.close(dIndex);
                                        });
                                    } else {
                                        layer.msg(res.msg, {time: 1000, icon: 2});
                                    }
                                }
                            });
                            return false;
                        });

                        // 监听取消按钮
                        $("button[lay-filter='closeAddDialog']").click(function () {
                            layer.close(dIndex);
                            return false;
                        });
                    },
                    end: function (index) {
                        // 重载表格，刷新数据
                        currTable.reload();
                    }
                });
            }
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var lineData = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑任务',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: false,
                    area: '580px', // 宽度580, 高度自适应
                    content: $('#editDialog').html(),
                    success: function (layero, dIndex) {

                        // 回显表单数据
                        form.val('edit-form', lineData);
                        form.render('select', 'edit-form');

                        // 表单提交事件
                        form.on('submit(editSaveBtn)', function (data) {
                            data.field.id = lineData.id;

                            console.log(JSON.stringify(data.field));

                            miniAjax.postJSON({
                                url: '/doc/edit',
                                data: JSON.stringify(data.field),
                                success: function (res) {
                                    if (res.code == '0') {
                                        layer.msg(res.msg, {
                                            time: 1000,
                                            icon: 1
                                        }, function () {
                                            layer.close(dIndex);
                                        });
                                    } else {
                                        layer.msg(res.msg, {time: 1000, icon: 2});
                                    }
                                }
                            });
                            return false;
                        });

                        // 监听取消按钮
                        $("button[lay-filter='closeEditDialog']").click(function () {
                            layer.close(dIndex);
                            return false;
                        });
                    },
                    end: function (index) {
                        // 重载表格，刷新数据
                        currTable.reload();
                    }
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确定删除此推送任务吗', function (index) {
                    layer.close(index);
                    miniAjax.get({
                        url: '/doc/delete?id=' + lineData.id,
                        success: function (res) {
                            if (res.code == '0') {
                                layer.msg(res.msg, {time: 1000, icon: 1});
                                // 重载表格，刷新数据
                                currTable.reload();
                            } else {
                                layer.msg(res.msg, {time: 1000, icon: 2});
                            }
                        }
                    });
                });
            }
        });

    });
</script>